/**
 * 郭楠project
 * com.gn.micblog.controller
 * DataController.java
 * 创建人:TonyGuo 
 * 时间：2016年6月5日-上午11:21:50 
 * 2016Tm公司-版权所有
 */
package com.gn.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gn.entity.AdminUser;
import com.gn.entity.R;
import com.gn.entity.User;
import com.gn.service.AdminUserService;
import com.gn.test.AdminUserTest;
import com.gn.util.CommonUtils;
import com.gn.util.IdUtils;

/**
 * 
 * DataController
 * 创建人:TonyGuo
 * 时间：2016年6月5日-上午11:21:50 
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("/rest/")
public class UserController {
	Logger logger = LoggerFactory.getLogger(UserController.class);
	@Resource
	private AdminUserService adminUserService;
	
	@RequestMapping("get.ip")
	public @ResponseBody String getIP(HttpServletRequest req){
		return CommonUtils.getIpAddr2(req);
	}
	
	/**
	 * 注册
	 * @param user
	 */
	@RequestMapping(value="signup",method=RequestMethod.GET)
	public @ResponseBody String addUser(HttpServletRequest req,
			AdminUser user,
			@RequestParam(value="numberyzm",required=true) String numberyzm){
		
		String yzmStr=(String)req.getSession().getAttribute("yzm");
		try {
			numberyzm=new String(numberyzm.getBytes("iso-8859-1"),"utf-8");
			if(numberyzm!=null&&numberyzm.equalsIgnoreCase(yzmStr)){
				user.setUserid(IdUtils.uuid());
				user.setUsercreatedate(CommonUtils.date2Str(new Date()));
				user.setOpenUserComment(0);
				user.setOpenUserMessage(0);
				user.setState(1);
				adminUserService.AddUser(user);
			}else if(numberyzm==null || "".equals(numberyzm)){
				return "vali_code_is_null";
			}else{
				return "vali_code_is_error";
			}
			logger.info("login success...{}",user.getUsername());
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return "signup_ok";
	}
	
	/**
	 * (登录)
	 * 方法名：login
	 * @param username
	 * @param password
	 * @since  1.0.0
	 * 根据IP获取地理位置
	 * http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=211.161.240.160
	 */
	@RequestMapping(value="logon",method=RequestMethod.POST)
	public @ResponseBody String login(HttpServletRequest req
			,@RequestParam(value="username",required=true) String username
			,@RequestParam(value="password",required=true) String password){
		
		
		List<AdminUser> users=adminUserService.findUserByUserName(username);
		AdminUser user=users.get(0);
		if(password.trim().equals(user.getUserpassword())){
			if(user!=null){
				user.setLoginDate(CommonUtils.date2Str(new Date()));
				adminUserService.update(user);
			}
			req.getSession().setAttribute("oatoken_", "login_ok");
			
			logger.info("users:{}",users.size());
			req.getSession().setAttribute("curr_user", user);
			return "true";
		}else{
			return "false";
		}
	}
	
	
	
	/**
	 * 查询所有用户列表
	 * @since  1.0.0
	 */
	@RequestMapping(value="user.findall")
	public @ResponseBody List<AdminUser> findAll(){
		System.out.println("users:{}"+adminUserService.findAll().size());
		for(AdminUser user: adminUserService.findAll()){
			System.out.println("usernmae:"+user.getUsername());
		}
		return adminUserService.findAll();//adminUserService.findAll()
	}
	
	/**
	 * 查询用户信息
	 */
	@RequestMapping(value="user.find",method=RequestMethod.GET)
	public @ResponseBody AdminUser findUserByUid(HttpServletRequest req){
		
		AdminUser user=(AdminUser)req.getSession().getAttribute("curr_user");
		String uid=user.getUserid();
		logger.info(" 查询用户信息 user:{}",adminUserService.findUserByUid(uid).getUsername());
		return uid.trim()!=null?adminUserService.findUserByUid(uid):null;
	}
	
	
	
	/**
	 * (验证昵称是否可用)
	 * @since  1.0.0
	 */
	@RequestMapping(value="/checknick.do",method=RequestMethod.GET)
	@ResponseBody
	public String CheckUserNick(HttpServletRequest request){
		Logger logger=LoggerFactory.getLogger(AdminUserTest.class);
		String nick="";
		try {
			nick=request.getParameter("usernick");
			String nickStr=new String(nick.getBytes("iso-8859-1"),"utf-8");
			logger.info("nick1******...{}"+nickStr);
			List<AdminUser> adminUser=adminUserService.findUserByUserName(nickStr);
			if(adminUser!=null || adminUser.size()!=0){
				logger.info("adminUser1....{}"+adminUser);
				return "true";	////true表示数据库中已经存在该昵称
			}else{
				logger.info("adminUser2....{}"+adminUser);
				return "false";  ////true表示数据库中不存在该昵称么，可以注册
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "false";
	}
	
	
	
	
}
